var url = null;

$(function() {
	loadStatus();
});

function loadStatus() {
	$('#status_query').combobox({
		url : getHttpRequestPath("/order/loadStatus"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadSku() {
	$('#sku_text').combobox({
		url : getHttpRequestPath("/baseInfo/loadSku"),
		valueField : 'value',
		textField : 'name'
	});
}

function loadZone() {
	$('#zone_text').combobox({
		url : getHttpRequestPath("/baseInfo/loadZone"),
		valueField : 'value',
		textField : 'name'
	});
}

/**
 * 查询
 */
function query() {
	var cmd = new Cmd();
	var orderId = $("#orderId_query").val();
	cmd.orderId = orderId.length > 0 ? orderId : null;
	var receiver = $("#receiver_query").val();
	cmd.receiver = receiver.length > 0 ? receiver : null;
	var statusCode = $("#status_query").combobox('getValue');
	cmd.statusCode = statusCode.length > 0 ? new Number(statusCode) : 0;
	if (orderId.length == 0 && receiver.length == 0 && statusCode.length == 0) {
		$.messager.alert('系统提示', '请输入查询条件!');
		return;
	}
	$('#dg').datagrid({
		view: detailview,
		url : getHttpRequestPath("/order/queryOrdersList"),
		pageNumber : 1,
		queryParams : {
			cmd : JSON.stringify(cmd, cmd.StringifyReplacer)
		},
		detailFormatter : function(index, row) {
			var ret = '';
			$.ajax({
				url:getHttpRequestPath("/transport/getTrans"),
				data:{
					"transId":row.transId,
				},
				success:function(response) {
					var order = JSON.parse(response, function(key, value) {
						return value;
					});
					ret += "配送跟踪：" + order.arrive;
				},
				async:false
			});
			return ret;
		}
	});
}

/**
 * 增加订单
 */
function initAdd() {
	url = getHttpRequestPath("/order/addOrder");
	clearData();
	loadZone();
	loadSku();
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '增加运单');
}

/**
 * 修改订单
 */
function initEdit(orderId) {
	url = getHttpRequestPath("/order/editOrder");
	clearData();
	loadZone();
	loadSku();
	getOrder(orderId);
	$('#dlg').dialog({
		modal : true
	}).dialog('open').dialog('setTitle', '修改运单' + orderId);
}

function clearData() {
	$('#orderId_text').val("");
	$('#sku_text').combobox("setValue", "");
	$('#amount_text').numberbox("setValue", "");
	$('#zone_text').combobox("setValue", "");
	$('#address_text').val("");
	$('#receiver_text').val("");
	$('#phone_text').val("");
}

/**
 * 初始化表单数据
 * 
 * @param row
 */
function initOrder(order) {
	$('#orderId_text').val(order.orderId);
	$('#sku_text').combobox("setValue", order.sku);
	$('#amount_text').numberbox("setValue", order.amount);
	$('#zone_text').combobox("setValue", order.zoneCode);
	$('#address_text').val(order.address);
	$('#receiver_text').val(order.receiver);
	$('#phone_text').val(order.phone);
}

function formatProduct(val, row, index) {
	var str = "";
	str += row.sku + " * " + row.amount;
	return str;
}

function formatAddress(val, row, index) {
	var str = "";
	$.ajax({
		url:getHttpRequestPath("/baseInfo/getZone"),
		data:{
			"zoneCode":row.zoneCode,
		},
		success:function(response) {
			var zone = JSON.parse(response, function(key, value) {
				return value;
			});
			str += zone.country + "-" + zone.province + "-" + zone.city + "<br>";
		},
		async:false
	});
	str += row.address;
	return str;
}

function formatReceiver(val, row, index) {
	var str = "";
	str += row.receiver + "<br>"
	str += row.phone;
	return str;
}

var formatDateStr = "yyyy-MM-dd hh:mm:ss";
function formatDate(val, row, index) {
	var str ="";
	str += new Date(row.create.time).format(formatDateStr) + "<br>";
	if (row.complete.time == "Jan 1, 1970 8:00:00 AM") {
		str += "-";
	} else {
		str += new Date(row.complete.time).format(formatDateStr);
	}
	return str;
}

/**
 * 操作
 */
function formatAction(val, row, index) {
	var str = "";
	if (row.status == 'WAITING') {
		str += createLinkElement('编辑', 'initEdit(\'' + row.orderId + '\')', 'icon-edit');
		str += createLinkElement('取消', 'cancel(\'' + row.orderId + '\')', 'icon-cancel');
	}
	return str;
}

function formatAction2(val, row, index) {
	var str = "";
	if (row.status == 'WAITING') {
		str += createLinkElement('编辑', 'initEdit(\'' + row.orderId + '\')', 'icon-edit');
		str += createLinkElement('审核', 'verify(\'' + row.orderId + '\')', 'icon-search');
		str += createLinkElement('取消', 'cancel(\'' + row.orderId + '\')', 'icon-cancel');
	}
	return str;
}

function formatAction3(val, row, index) {
	var str = "";
	if (row.status == 'DISPATCH') {
		str += createLinkElement('签收', 'confirm(\'' + row.orderId + '\')', 'icon-edit');
	}
	return str;
}